package Top_Interview_Questions.Stack;

import Top_Interview_Questions.Stack.Supple.NestedInteger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: 吕庆龙
 * @Date: 2020/1/8 14:19
 * <p>
 * 功能描述:
 */
public class _0341 {
    public static void main(String[] args) {

    }

}

class NestedIterator implements Iterator<Integer> {

    private List<Integer> list;
    private int index;

    public NestedIterator(List<NestedInteger> nestedList) {
        list = initList(nestedList);
    }

    @Override
    public Integer next() {
        if(this.hasNext())
            return list.get(index++);
        return null;
    }

    @Override
    public boolean hasNext() {
        if (index < list.size())
            return true;
        return false;
    }

    public static List<Integer> initList(List<NestedInteger> nestedList){
        List<Integer> list1 = new ArrayList<>();
        for(NestedInteger n:nestedList){
            if(n.isInteger()){
                list1.add(n.getInteger());
            }else{
                list1.addAll(initList(n.getList()));
            }
        }
        return list1;
    }
}

